package com.hkts.flow.manager.api;

import com.dragon.tools.common.ReturnCode;
import com.dragon.tools.common.UUIDGenerator;
import com.dragon.tools.vo.ReturnVo;
import com.google.gson.Gson;
import com.hkts.flow.api.flowable.StartProcessInstanceVo;
import com.hkts.flow.api.flowable.custom.FormAndItemResultVo;
import com.hkts.flow.api.flowableTo.HontUserTo;
import com.hkts.flow.api.flowableTo.PerformanceTo;
import com.hkts.flow.api.modelleave.Purchase;
import com.hkts.flow.api.performanceVo.FlowableVaribleVo;
import com.hkts.flow.core.service.flowable.IFlowableProcessInstanceService;
import com.hkts.flow.core.service.flowable.IFlowableTaskService;
import com.hkts.flow.feign.FeignInterface;
import com.hkts.flow.feign.FeignPerformance;
import com.hkts.flow.manager.utils.Result;
import com.hkts.flow.manager.utils.ResultUtil;
import feign.Body;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.idm.api.User;
import org.flowable.ui.common.security.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassNamePerformanceResource
 * @Description
 * @Author yck
 * @Date2020-12-17 8:15
 * @Version V1.0
 **/
@RestController
@RequestMapping("/rest/performance")
public class PerformanceResource extends BaseResource{
    private static Logger logger = LoggerFactory.getLogger(PurchaseResource.class);
    private final String nameSpace = "performance";

    @Autowired
    private IFlowableProcessInstanceService flowableProcessInstanceService;
    @Autowired
    private FeignInterface feignInterface;

    @Autowired
    private IFlowableTaskService flowableTaskService;
    //启动流程
    @PostMapping("/add")
    @Transactional
    public String add( String hontUserId) throws Exception {

        HontUserTo hontUserTo = feignInterface.UserInfo();
        Result<Map<String,Object>> result = feignInterface.performanceUserInfo();
        Map<String, Object> result1 = result.getResult();
        System.out.println("用户角色："+result1.get("identity"));
        if(result1.get("identity").toString().equals("5")){
           /* FlowableVaribleVo varibleVo=new  FlowableVaribleVo(returnStart.getData().getProcessInstanceId(),"initiator","5");
            flowableTaskService.updateVar(varibleVo);*/
        }
        PerformanceTo vo=new PerformanceTo();
        String purchaseId = UUIDGenerator.generate();
        StartProcessInstanceVo startProcessInstanceVo = new StartProcessInstanceVo();
        startProcessInstanceVo.setBusinessKey(purchaseId);
        startProcessInstanceVo.setCreator(hontUserTo.getId());
        startProcessInstanceVo.setCurrentUserCode(hontUserTo.getId());
        startProcessInstanceVo.setFormName("绩效考核流程");
        startProcessInstanceVo.setSystemSn("flow");
        startProcessInstanceVo.setProcessDefinitionKey("performance");
        Map<String, Object> variables = new HashMap<>();
        variables.put("userOrgCode",hontUserTo.getDepartmentId());
        //2021/4/6.
        variables.put("orgCode",hontUserTo.getDepartmentId().substring(0,2));

        variables.put("identity",result1.get("identity").toString());
        startProcessInstanceVo.setVariables(variables);
        ReturnVo<ProcessInstance> returnStart = flowableProcessInstanceService.startProcessInstanceByKey(startProcessInstanceVo);
        System.out.println(returnStart.getData().getProcessInstanceId());
        return returnStart.getData().getProcessInstanceId();
    }
}
